#include <Core/logger/logger.h>
#include "connectorthread.h"
#include "sslclient.h"

//#include <protocol/message.h>

namespace Opl
{
    namespace Helpers
    {

        class ConnectorThreadPrivate
        {
            public:
                ConnectorThreadPrivate(): stopRequired(false), client(0) {}
                ~ConnectorThreadPrivate() {}

                bool stopRequired;
                SslClient* client;
                ConnectorSetup setup;
                void clear()
                {
                }
        };

        ConnectorThread* ConnectorThread::instance()
        {
            static ConnectorThread thread;// QSharedPointer<ConnectorThread> thread(new ConnectorThread);
            return &thread;
        }

        ConnectorThread::ConnectorThread(QObject* parent) :
            QThread(parent), O_PRIVATE_CREATE(ConnectorThread)
        {
            O_D(ConnectorThread);
            //d->
        }

        ConnectorThread::~ConnectorThread()
        {
            O_PRIVATE_DELETE;
        }


        void ConnectorThread::stop()
        {
            O_D(ConnectorThread);
            LI.Info() << "Connector Thread stop";
            d->stopRequired = true;
            d->client->stop();
            exit();
        }

        void ConnectorThread::Setup(const ConnectorSetup& setup)
        {
            O_D(ConnectorThread);
            LI.Info("Connector::Setup");
            d->setup = setup;
        }

        void ConnectorThread::Send(const QString& data)
        {
            O_D(ConnectorThread);
            if (!d->client)
            {
                LI.Error("ConnectorThread::Send. Client not initialized");
                return;
            }

            d->client->Send(data);
        }

        void ConnectorThread::clientConnected()
        {
            O_D(ConnectorThread);
            LI.Info("ConnectorThread::clientConnected()");
            emit connected();
        }

        void ConnectorThread::clientDisconnected()
        {
            O_D(ConnectorThread);
            LI.Info("ConnectorThread::clientConnected()");
            emit disconnected();
        }

        void ConnectorThread::connectionError()
        {
            emit error();
        }

        void ConnectorThread::run()
        {
            O_D(ConnectorThread);
            LI.Info("ConnectorThread::run()");

            if (!d->client)
            {
                d->client = new SslClient(d->setup.host, d->setup.port);
                d->client->moveToThread(this);
                connect(d->client, SIGNAL(connected()), this, SLOT(clientConnected()));
                connect(d->client, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
                connect(d->client, SIGNAL(error()), this, SLOT(connectionError()));
                d->client->start();
            }

            exec();
        }

    }
}
